<!-- 办理报名等选择课程 -->
<template>
  <div>

    <el-dialog title="选择课程" :visible.sync="open" width="1200px"  class="compact">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="75px" :rules="rules" class="compact">
        <el-form-item label="选择学员" prop="studentName">
          <student-select v-model="form.studentName" placeholder="查询学生"  @change="handleStudentChange" ></student-select>
          <template>
            <div class="red-text">按最新报名从上到下排序</div>
          </template>
        </el-form-item>

        <el-form-item label="课程名称:" prop="courseName">
          <el-input
            v-model="queryParams.coursename"
            placeholder="请输入课程名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>





            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>

<!--          <el-button :loading="loading" type="primary" @click="chooseCourse">确定选择</el-button>-->

      </el-form>





      <el-table ref="table" v-loading="loading"  :data="dataList" height="400"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="班级名称" align="center" prop="claName" />
        <el-table-column label="教师名" align="center" prop="name" />
        <el-table-column label="课程名" align="center" prop="coursename" />
        <el-table-column label="人数" align="center" prop="capacity" />
        <el-table-column label="每次扣除课时" align="center" prop="everyStuLoseHour" />

        <el-table-column label="上课类型" align="center" prop="clssstype">
          <template slot-scope="scope">
            <span v-if="scope.row.clsssType === '0'">一对一</span>
            <span v-else-if="scope.row.clsssType === '2'">班课</span>
          </template>
        </el-table-column>
        <el-table-column label="总课时" align="center" prop="totaltime" />

        <el-table-column label="开班日期" align="center" prop="openDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.openDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结班日期" align="center" prop="closeDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.closeDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>



      </el-table>



      <div slot="footer" class="dialog-footer" >
          <div class="footer-left-txt">

          </div>
          <div class="align-right">
            <el-button :loading="loading" type="primary" @click="chooseCourse">确定选择</el-button>
            <el-button @click="open = false">取 消</el-button>
          </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listCla as selectCourseListWithStudentCourse } from '@/api/system/cla'
import studentSelect from '@/components/Student/studentSelect'
import {getStudent} from "@/api/system/student";

export default {
  components:{
    studentSelect
  },
  dicts: ['sys_course_status'],
  data() {

    return {
      open: false,
      loading: true,
      loadingSelect: false,
      // 总条数
      total: 0,
      // 表格树数据
      dataList: [],


      rules:{


      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName:undefined,
        coursename: undefined,
        departId: undefined,
        courseTypeId: undefined,
        teachingMode: undefined,
        chargeType: undefined,
        sale: '1'
      },

      //多选框
      selection:{

      },
      form:{
        studentName: '',
        studentId:''

      },
      // 课程类型
      courseTypeOptions: [],
      // 教学模式
      teachingModeOptions: [{
        teachingMode: '1',
        teachingModeName: '班课'
      }, {
        teachingMode: '2',
        teachingModeName: '一对一'
      }],
      // 开课校区
      campusOptions: [],
      // 收费模式
      chargeTypeOptions: [{
        chargeType: 'hour',
        chargeTypeName: '按课时'
      }, {
        chargeType: 'date',
        chargeTypeName: '按时间'
      }, {
        chargeType: 'cycle',
        chargeTypeName: '按周期'
      }],
      chooseCourseIdList: [],
      hasMore: false
    }
  },
  created() {
    this.getList();
  },
  methods: {

    handleStudentChange(val){
      if (val !== null && val !== ''){

        this.form.studentId=val

      getStudent(val).then(response => {
        this.form.studentName=response.data.name


      })
    }else {
        this.form.studentId=''
        this.form.studentName=''
      }



      },


      // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection=selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },



    courseTypeList() {
      courseTypeSelect({}).then(response => {
        this.courseTypeOptions = response.data
      })
    },
    async campusList() {
      const response = await campusList()
      this.campusOptions = response.data
      if (this.campusOptions.length > 0) {
        this.queryParams.departId = this.campusOptions[0].id
      }
      this.getList()
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      selectCourseListWithStudentCourse(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;

        this.loading = false



      })
    },
    loadMore() {
      if (this.hasMore) {
        this.queryParams.pageNum = this.queryParams.pageNum + 1
        this.getList()
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.dataList = []
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    chargeNameByCodes(chargeNames) {
      if (chargeNames) {
        const chargeNameArray = []
        chargeNames.split(',').forEach((value, index, array) => {
          if (value === 'hour') {
            chargeNameArray.push('按课时')
          } else if (value === 'date') {
            chargeNameArray.push('按时间')
          } else if (value === 'cycle') {
            chargeNameArray.push('按周期')
          }
        })
        return chargeNameArray.toString()
      } else {
        return '-'
      }
    },
    handleSelect(selection, row) {
      const idx = this.chooseCourseIdList.indexOf(row.courseId)
      if (idx !== -1) {
        this.chooseCourseIdList.splice(idx, 1)
      } else {
        this.chooseCourseIdList.push(row.courseId)
      }
    },
    handleSelectAll(selection) {
      this.chooseCourseIdList = selection.map(item => item.courseId)
    },
    chooseCourse() {

      if (this.form.studentId!==null && this.form.studentId!=='' && this.form.studentId!==undefined){
      this.selection[0].studentName=this.form.studentName
      this.selection[0].studentId=this.form.studentId
      this.$emit('chooseComplete', {
        selection: this.selection,
      })
        this.resetQuery(),
          this.open = false
      }
      else {
        this.msgError('请选择学生')
      }

    }
  }
}
</script>
<style lang="scss" scoped>

.errorMessage {
  display: none;
  color: red;
  font-size: 12px;
  margin-top: 5px;
}
  .text-right {
    text-align: right;
  }
  .append-table-loading {
    text-align: center;
    color: #848484;
    padding: 5px;
    font-size: 13px;
  }
  .dialog-footer {

    .footer-left-txt {
      line-height: 36px;
      color: #606266;
      font-size: 13px;
    }
  }
  // 右对齐
  .align-right {
    text-align: right;
  }
  .flex {
    display: flex;
    &.space-between {
      justify-content: space-between;
    }
  }
  // 紧凑form
  .compact {
    margin-bottom: 5px;

  }



  .compact{
    padding: 0px;
  }
.red-text{
  color: red;
  position: absolute;
  top: -30px;
  left: 5px;
}
</style>
